﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DTO;
using System.Data;

namespace DAO
{
    public class NhanVienDAO : AbstractDAO<DTO.NhanVienDTO>
    {
        protected override string GetTableName()
        {
            return "nhanvien";
        }

        public override DTO.NhanVienDTO GetByDataRow(System.Data.DataRow data)
        {
            DTO.NhanVienDTO nv = new DTO.NhanVienDTO();
            int id, loai;
            int.TryParse(data["id"].ToString(), out id);
            int.TryParse(data["loai"].ToString(), out loai);

            nv.TenDangNhap = data["tendangnhap"].ToString();
            nv.MatKhau = data["matkhau"].ToString();
            nv.Email = data["email"].ToString();

            nv.Id = id;
            nv.Loai = loai;

            return nv;
        }

        public int DangNhap(string tenDangNhap, string matKhau)
        {
            IList<SqlParameter> listParameter = new List<SqlParameter>();
            listParameter.Add(new SqlParameter("@TenDangNhap", tenDangNhap));
            listParameter.Add(new SqlParameter("@MatKhau", matKhau));
            SqlParameter sqlThongBao = new SqlParameter("@ThongBao", SqlDbType.Int );
            sqlThongBao.Direction = System.Data.ParameterDirection.Output;
            listParameter.Add(sqlThongBao);
            this.dataProvider.ExecuteStoreProc(
                "Proc_DangNhap",
                listParameter);
            return (int)sqlThongBao.Value;
        }

        public NhanVienDTO getByTenDangNhap(string tenDangNhap)
        {
            NhanVienDTO nv = null;
            string tableName = GetTableName();
            String query = String.Format(
                "SELECT * FROM {0} WHERE tendangnhap = '{1}'",
                tableName,
                tenDangNhap);
            DataTable dt = dataProvider.ExecuteQuery(query);
            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                nv = GetByDataRow(row);
            }
            return nv;
        }

        public bool AddNew(DTO.NhanVienDTO nhanvienDTO)
        {
            IList<SqlParameter> listParameter = new List<SqlParameter>();
            listParameter.Add(new SqlParameter("@TenDangNhap", nhanvienDTO.TenDangNhap));
            listParameter.Add(new SqlParameter("@MatKhau", nhanvienDTO.MatKhau));
            listParameter.Add(new SqlParameter("@Email", nhanvienDTO.Email));
            listParameter.Add(new SqlParameter("@Loai", nhanvienDTO.Loai));
            SqlParameter sqlThongBao = new SqlParameter("@ThanhCong", SqlDbType.Bit);
            sqlThongBao.Direction = System.Data.ParameterDirection.Output;
            listParameter.Add(sqlThongBao);

            this.dataProvider.ExecuteStoreProc(
                "Proc_ThemNhanVien",
                listParameter);
            return (bool)sqlThongBao.Value;
        }

        public bool Edit(DTO.NhanVienDTO nhanvienDTO)
        {
            IList<SqlParameter> listParameter = new List<SqlParameter>();
            listParameter.Add(new SqlParameter("@Id", nhanvienDTO.Id));
            listParameter.Add(new SqlParameter("@TenDangNhap", nhanvienDTO.TenDangNhap));
            listParameter.Add(new SqlParameter("@MatKhau", nhanvienDTO.MatKhau));
            listParameter.Add(new SqlParameter("@Email", nhanvienDTO.Email));
            listParameter.Add(new SqlParameter("@Loai", nhanvienDTO.Loai));
            SqlParameter thongBao = new SqlParameter("@ThanhCong", System.Data.SqlDbType.Bit);
            thongBao.Direction = System.Data.ParameterDirection.Output;
            listParameter.Add(thongBao);

            this.dataProvider.ExecuteStoreProc(
                "Proc_CapNhatNhanVien",
                listParameter);
            return (bool)thongBao.Value;
        }

        public bool CheckUsername(String username)
        {

            return false;
        }

        public bool CheckEmail(String email)
        {

            return false;
        }
    }
}
